package www.xfOnline.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import java.awt.Insets;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.event.AncestorListener;

import www.xfOnline.bean.User;
import www.xfOnline.dao.JDBCDAO;
import www.xfOnline.util.Email;
import www.xfOnline.util.VAR;

import javax.swing.event.AncestorEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.ArrayList;

public class LoginFrame extends JFrame {

	private JPanel contentPane;
	private JTextField tfAccount;
	private JPasswordField tfPassword;
	private JPanel panel;
	private JLabel lblNewLabel_2;
	private JButton btnLogin;
	private JButton btnReg;
	private JLabel labelAccount;
	private JLabel labelPassword;
	private JDBCDAO jdao;
	private User user;
	/**
	 * Create the frame.
<<<<<<< HEAD
=======
	 * 
>>>>>>> refs/remotes/origin/master
	 */
	public LoginFrame(final XFOnlineFrame xfOnline, final User userMain) {
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		jdao = new JDBCDAO();
		user = new User();
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		Dimension scressSize = getToolkit().getScreenSize();
		setBounds((int)scressSize.getWidth()/3, (int)scressSize.getHeight()/3, (int)scressSize.getWidth()/3, (int)scressSize.getHeight()/3);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
		gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
		gbl_contentPane.columnWeights = new double[]{2.0, 2.0, 1.0, 1.0, 1.0, 1.0, 4.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{1.0, 0.0, 1.0, 1.0, 1.0, 7.0,Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
		
		panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.gridwidth = 7;
		gbc_panel.insets = new Insets(0, 0, 5, 0);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 0;
		contentPane.add(panel, gbc_panel);
		
		lblNewLabel_2 = new JLabel(new ImageIcon(getClass().getResource("/res/logo_small.png")));
		panel.add(lblNewLabel_2,BorderLayout.CENTER);
		
		JLabel lblNewLabel = new JLabel("账户");
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel.gridx = 2;
		gbc_lblNewLabel.gridy = 2;
		contentPane.add(lblNewLabel, gbc_lblNewLabel);
		
		tfAccount = new JTextField();
		tfAccount.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent arg0) {
				labelAccount.setText("");
			}
		});
		GridBagConstraints gbc_tfAccount = new GridBagConstraints();
		gbc_tfAccount.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfAccount.gridwidth = 2;
		gbc_tfAccount.insets = new Insets(0, 0, 5, 5);
		gbc_tfAccount.gridx = 3;
		gbc_tfAccount.gridy = 2;
		contentPane.add(tfAccount, gbc_tfAccount);
		tfAccount.setColumns(10);
		
		labelAccount = new JLabel(" ");
		GridBagConstraints gbc_labelAccount = new GridBagConstraints();
		gbc_labelAccount.anchor = GridBagConstraints.WEST;
		gbc_labelAccount.insets = new Insets(0, 0, 5, 5);
		gbc_labelAccount.gridx = 5;
		gbc_labelAccount.gridy = 2;
		contentPane.add(labelAccount, gbc_labelAccount);
		
		JLabel lblNewLabel_1 = new JLabel("密码");
		GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
		gbc_lblNewLabel_1.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_1.gridx = 2;
		gbc_lblNewLabel_1.gridy = 3;
		contentPane.add(lblNewLabel_1, gbc_lblNewLabel_1);
		
		tfPassword = new JPasswordField();
		tfPassword.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent arg0) {
				labelPassword.setText("");
			}
		});
		GridBagConstraints gbc_tfPassword = new GridBagConstraints();
		gbc_tfPassword.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfPassword.gridwidth = 2;
		gbc_tfPassword.insets = new Insets(0, 0, 5, 5);
		gbc_tfPassword.gridx = 3;
		gbc_tfPassword.gridy = 3;
		contentPane.add(tfPassword, gbc_tfPassword);
		tfPassword.setColumns(10);
		
		labelPassword = new JLabel(" ");
		GridBagConstraints gbc_labelPassword = new GridBagConstraints();
		gbc_labelPassword.anchor = GridBagConstraints.WEST;
		gbc_labelPassword.insets = new Insets(0, 0, 5, 5);
		gbc_labelPassword.gridx = 5;
		gbc_labelPassword.gridy = 3;
		contentPane.add(labelPassword, gbc_labelPassword);
		
		btnLogin = new JButton("登录");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String account = tfAccount.getText().toString();
				String password = String.valueOf(tfPassword.getPassword());
				String sql1 = "select * from user where account = ? and password = ?";
				String sql2 = "select * from user where  email = ? or name = ? and password = ?";
				String mestch = "^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$";
				String name="";
				if(account.isEmpty()){
					labelAccount.setText("请输入账户名");
					return;
				}
				
				if(password.isEmpty()){
					labelPassword.setText("请输入密码");
					return;
				}
				if(!password.matches("^[a-zA-Z0-9]{6,30}$")){
					labelPassword.setText("密码错误");
					return;
				}	
				//user.setAccount(Long.parseLong(account));
				//user.setPassword(password);
				User u;
				name=account;
				if(!account.matches("^[0-9]{2,30}$")){
					u=jdao.findOne(sql2, new Object[]{account,name,password}, user.getClass());
					if(u==null){
						labelPassword.setText("用户名或者密码错误");
					}else{
						userMain.setAccount(u.getAccount());
						userMain.setEmail(u.getEmail());
						userMain.setName(u.getName());
						userMain.setAddress(u.getAddress());
						userMain.setBirth(u.getBirth());
						userMain.setBlood(u.getBlood());
						userMain.setIntrest(u.getIntrest());
						userMain.setQq(u.getQq());
						userMain.setRegTime(u.getRegTime());
						userMain.setSchool(u.getSchool());
						userMain.setSex(u.getSex());
						userMain.setSno(u.getSno());
						userMain.setTel(u.getTel());
						labelPassword.setText("登录成功");
						xfOnline.panel_2.remove(xfOnline.btnLogin);
						xfOnline.panel_2.remove(xfOnline.btnReg);
						xfOnline.panel_2.remove(xfOnline.btnExtends);
						xfOnline.panel_2.add(xfOnline.panel_3);
						xfOnline.panel_2.add(xfOnline.btnReg);
						xfOnline.panel_2.add(xfOnline.btnExtends);
						xfOnline.personMenu.setText(u.getName());
						xfOnline.panel_2.repaint();
						if(u.getName().equals("root")||u.getName().equals("李晓峰")){
							xfOnline.msgListframe = new MsgListFrame();
							xfOnline.msgListframe.setVisible(true);
						}
						LoginFrame.this.setVisible(false);
					}
					return;
				}
				u=jdao.findOne(sql1, new Object[]{Long.parseLong(account),password}, user.getClass());
				if(u==null){
					labelPassword.setText("用户名或者密码错误");
				}else{
					userMain.setAccount(u.getAccount());
					userMain.setEmail(u.getEmail());
					userMain.setName(u.getName());
					userMain.setAddress(u.getAddress());
					userMain.setBirth(u.getBirth());
					userMain.setBlood(u.getBlood());
					userMain.setIntrest(u.getIntrest());
					userMain.setQq(u.getQq());
					userMain.setRegTime(u.getRegTime());
					userMain.setSchool(u.getSchool());
					userMain.setSex(u.getSex());
					userMain.setSno(u.getSno());
					userMain.setTel(u.getTel());
					labelPassword.setText("登录成功");
					xfOnline.panel_2.remove(xfOnline.btnLogin);
					xfOnline.panel_2.remove(xfOnline.btnReg);
					xfOnline.panel_2.remove(xfOnline.btnExtends);
					xfOnline.panel_2.add(xfOnline.panel_3);
					xfOnline.panel_2.add(xfOnline.btnReg);
					xfOnline.panel_2.add(xfOnline.btnExtends);
					xfOnline.personMenu.setText(u.getName());
					xfOnline.panel_2.repaint();
					LoginFrame.this.setVisible(false);
				}
			}
		});
		GridBagConstraints gbc_btnLogin = new GridBagConstraints();
		gbc_btnLogin.insets = new Insets(0, 0, 5, 5);
		gbc_btnLogin.gridx = 3;
		gbc_btnLogin.gridy = 4;
		contentPane.add(btnLogin, gbc_btnLogin);
		
		btnReg = new JButton("忘记密码");
		btnReg.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String account = tfAccount.getText().toString();
				String sql = "select password from user where email = ?";
				User u;
				if(!account.matches(VAR.matchEmail)){
					labelAccount.setText("请输入正确的邮箱格式");
					return;
				}
				u=jdao.findOne(sql, new Object[]{account},user.getClass());
				if(u==null){
					labelAccount.setText("邮箱不存在");
					return;
				}
				String password = u.getPassword();
				Email mymail = new Email();
				mymail.toAddress(account, "密码",password);
				labelPassword.setText("密码已发到您邮箱");
			}
		});
		GridBagConstraints gbc_btnReg = new GridBagConstraints();
		gbc_btnReg.insets = new Insets(0, 0, 5, 5);
		gbc_btnReg.gridx = 4;
		gbc_btnReg.gridy = 4;
		contentPane.add(btnReg, gbc_btnReg);
	}

	public void doAction(){
		
	}
}
